(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.classification.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'classificationService' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, classificationService) {
		var ctrl = this, classificationInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			var params = {
					classification : classificationInfo.siteCode
			};
			ctrl.propClassifiInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.propClassifi.tableConfigs.table, rowSelectionChanged);
			ctrl.propClassifiInnerGridOptions.data = 'vm.classification.classifiList';
			ctrl.propClassifiInnerGridOptions.verifyData = 'vm.classificationOld.classifiList';
			ctrl.propClassifiInnerGridOptions.enableRowHeaderSelection = false;
			
			authService.updateVerify($scope.$stateParams.params).then(function(response) {
					ctrl.classification = response.data.model.newModel;
					ctrl.classification.siteCode = ctrl.classification.siteCode;
					ctrl.classification.siteName = ctrl.classification.siteName;
					ctrl.classification.dataStatus = params.dataStatus || 'V';
					ctrl.classificationOld = response.data.model.oldModel;
					ctrl.classificationOld.classifiList = ctrl.classificationOld.classifiList || [];
					ctrl.classification.classifiList = ctrl.classification.classifiList || [];
					ctrl.classification.taskId = response.data.task.taskId;
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedClassifications = selectedRows;
		}
		
		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.classification.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.classification).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.classification.comments = comments;
						authService.updateVerifyReject(ctrl.classification).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.classification.list',
					'state' : 'cda.classification.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.classification.list',
					'state' : 'cda.classification.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);